package com.bootdo.per.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.common.annotation.Log;
import com.bootdo.common.config.Constant;
import com.bootdo.common.controller.BaseController;
import com.bootdo.common.utils.R;
import com.bootdo.per.domain.WeightDO;
import com.bootdo.per.domain.WeightModel;
import com.bootdo.per.service.WeightService;
/**
 * 
 * @author HonestWan
 *
 */
@RequestMapping("/per/weight")
@Controller
public class WeightController extends BaseController {
	String prefix = "per/weight";
	
	@Autowired
	private WeightService weightService;
	
	@RequiresPermissions("per:weight:weight")
	@GetMapping()
	String weight() {
		return prefix + "/weight";
	}
	
	@RequiresPermissions("per:weight:weight")
	@GetMapping("/list")
	@ResponseBody()
	List<WeightDO> list() {
		List<WeightDO> weights = weightService.list();
		return weights;
	}
	
	@Log("重置")
	@RequiresPermissions("per:weight:reset")
	@GetMapping("/reset")
	String reset(Model model) {
		List<WeightDO> weights = weightService.list();
		model.addAttribute("wList", weights);
		return prefix + "/reset";
	}
	
	@RequiresPermissions("per:weight:batchUpdate")
	@Log("批量修改权重")
	@PostMapping("/batchUpdate")
	@ResponseBody
	R brachUpdate(WeightModel list){
		if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
			return R.error(1, "演示系统不允许修改,完整体验请部署程序");
		}
		int r = weightService.batchUpdate(list);
		if (r > 0) {
			return R.ok();
		}
		return R.error();
	}
}
